* Data between 2004-2015
use "/data2/cedsei/data2/DS_Lev_AF_hist_aktso.dta",clear  

tab senaste_kontakt_typ, gen(kontakt_typ)
generate daydate = date(senaste_kontakt_dat, "YMD")
gen year = year(daydate)
keep if inrange(year,2004,2015)


gcollapse  (max)  kontakt_typ* ,by(lopnr daydate)
gen date = ym(year(daydate),month(daydate))

gcollapse  (sum) kontakt_typ* , by(lopnr date)

*Save data
rename lopnr persid
compress
save "$datapath/histold.dta",replace



*Data between 2016-2021
use "/data2/cedsei/data2/hist_aktso.dta",clear
tab SENASTE_KONTAKT_TYP, gen(kontakt_typ)
gen year = year(SENASTE_KONTAKT_DAT)
keep if inrange(year,2016,2019)

fcollapse (max)  kontakt_typ*  ,by(lopnr SENASTE_KONTAKT_DAT)
gen date = ym(year(SENASTE_KONTAKT_DAT),month(SENASTE_KONTAKT_DAT))
format date %tm

gcollapse  (sum) kontakt_typ* , by(lopnr date)

*Save data
rename lopnr persid
compress
save "$datapath/histnew.dta",replace



*Grand histaktso data
append using "$datapath/histold.dta"
save "$datapath/hist_v2.dta",replace

